package com.taxshare.purchaser.config;

import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.aop.interceptor.SimpleAsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

/**
 * 异步执行线程池配置
 *
 * @author luozhongyi
 * @date 2021/04/13
 */
@Configuration
@EnableAsync
@SuppressWarnings("all")
public class AsyncTreadPoolConfig implements AsyncConfigurer {

    @Override
    public Executor getAsyncExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        //初始线程池大小
        executor.setCorePoolSize(Runtime.getRuntime().availableProcessors() * 10);
        //最大线程数量
        executor.setMaxPoolSize(Runtime.getRuntime().availableProcessors() * 50);
        //线程池名称前缀
        executor.setThreadNamePrefix("Purchaser-");
        executor.initialize();
        return executor;
    }

    /**
     * 异步任务中异常处理
     */
    @Override
    public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
        return new SimpleAsyncUncaughtExceptionHandler();
    }

}
